var app = getApp(), Toptips = require("../../libs/zanui/toptips/index"), Toast = require("../../libs/zanui/toast/toast");

Page({
    data: {
        videoCache: !0,
        showPopup: !1,
        confirmBar: !1,
        disabled: !1,
        cancelWithMask: !0,
        actions: [ {
            name: "已转让",
            loading: !1
        }, {
            name: "下架",
            loading: !1
        }, {
            name: "编辑",
            loading: !1
        } ],
        actions2: [ {
            name: "已转让",
            loading: !1
        }, {
            name: "上架",
            loading: !1
        }, {
            name: "编辑",
            loading: !1
        } ],
        cancelText: "取消",
        tips: {
            top: "您需要登录之后才能进行下一步操作",
            bottom: "点击确定按钮登录"
        },
        defaultBtn: !1
    },
    onLoad: function(t) {
        var a = this;
        app.viewCount();
        var e = t.id, i = wx.getStorageSync("userInfo");
        if (i) {
            var s = i.memberInfo.uid;
            a.setData({
                uid: s,
                item_id: e
            });
        } else a.setData({
            item_id: e
        });
        app.util.footer(a), a.getItemDetail(e);
    },
    getItemDetail: function(t) {
        var e = this;
        app.util.request({
            url: "entry/wxapp/item",
            cachetime: "0",
            data: {
                act: "detail",
                id: t,
                m: "superman_hand2"
            },
            success: function(t) {
                if (t.data.errno) e.showIconToast(t.errmsg); else {
                    var a = t.data.data;
                    e.data.uid && e.data.uid == a.item.seller_uid && e.setData({
                        is_author: !0
                    }), 0 < a.item.is_favour && e.setData({
                        is_fav: !0
                    }), 0 < a.item.is_collect && e.setData({
                        is_collect: !0
                    }), -1 == a.item.status && e.setData({
                        soldOut: !0
                    }), 2 == a.item.status && e.setData({
                        disabled: !0
                    }), e.setData({
                        detail: a.item,
                        message: a.message
                    });
                }
            }
        });
    },
    getUserInfo: function(t) {
        var e = this;
        e.setData({
            showLogin: !1
        }), "getUserInfo:ok" == t.detail.errMsg && app.util.getUserInfo(function(t) {
            var a = t.memberInfo.uid;
            e.setData({
                uid: a
            }), e.data.liuyan && (e.setData({
                liuyan: !1
            }), e.leaveMsg()), e.data.huifu && (e.setData({
                huifu: !1
            }), e.replyMsg()), e.data.action && (e.setData({
                action: !1
            }), e.getItemDetail(e.data.item_id), e.itemAction()), e.data.action && (e.setData({
                action: !1
            }), e.itemAction()), e.data.chat && (e.setData({
                chat: !1
            }), e.getItemDetail(e.data.item_id), e.toChat());
        }, t.detail);
    },
    closeLogin: function() {
        this.setData({
            showLogin: !1
        });
    },
    previewImg: function(t) {
        var a = t.currentTarget.dataset.src;
        wx.previewImage({
            current: a,
            urls: this.data.detail.album
        });
    },
    showVideo: function(t) {
        var a = t.currentTarget.dataset.index, e = this.data.detail.video;
        this.setData({
            showPopup: !0,
            auto: !0,
            video: e[a]
        });
    },
    togglePopup: function() {
        this.setData({
            showPopup: !this.data.showPopup
        });
    },
    leaveMsg: function() {
        var t = this;
        t.data.uid ? t.setData({
            showModal: !0
        }) : t.setData({
            showLogin: !0,
            liuyan: !0
        });
    },
    replyMsg: function(t) {
        var a = this, e = t.currentTarget.dataset.id;
        a.data.uid ? a.setData({
            msg_id: e,
            showModal: !0
        }) : a.setData({
            showLogin: !0,
            huifu: !0
        });
    },
    itemAction: function(t) {
        var e = this, i = t.currentTarget.dataset.type, s = t.currentTarget.dataset.status, o = e.data.item_id;
        e.data.uid ? app.util.request({
            url: "entry/wxapp/item",
            cachetime: "0",
            data: {
                act: "detail",
                id: o,
                type: i,
                status: s,
                m: "superman_hand2"
            },
            success: function(t) {
                if (t.data.errno) e.showIconToast(t.errmsg); else {
                    var a = "";
                    1 == i ? s ? (a = "取消点赞", e.setData({
                        is_fav: !1
                    })) : a = "点赞成功" : s ? (a = "取消收藏", e.setData({
                        is_collect: !1
                    })) : a = "收藏成功", e.showIconToast(a, "success"), e.getItemDetail(o);
                }
            }
        }) : e.setData({
            showLogin: !0,
            action: !0
        });
    },
    closeModal: function() {
        this.setData({
            showModal: !1
        });
    },
    formSubmit: function(t) {
        var a = this, e = t.detail.value.content, i = a.data.item_id;
        if ("" != e) if (a.data.msg_id) {
            var s = a.data.msg_id;
            if (!s) return;
            app.util.request({
                url: "entry/wxapp/item",
                cachetime: "0",
                data: {
                    act: "detail",
                    id: i,
                    msg_id: s,
                    reply: e,
                    m: "superman_hand2"
                },
                success: function(t) {
                    a.setData({
                        showModal: !1
                    }), t.data.errno ? a.showIconToast(t.errmsg) : (a.showIconToast("回复成功", "success"), 
                    a.getItemDetail(i));
                }
            });
        } else app.util.request({
            url: "entry/wxapp/item",
            cachetime: "0",
            data: {
                act: "detail",
                id: i,
                comment: e,
                m: "superman_hand2"
            },
            success: function(t) {
                a.setData({
                    showModal: !1
                }), t.data.errno ? a.showIconToast(t.errmsg) : (a.showIconToast("提交成功", "success"), 
                a.getItemDetail(i));
            }
        }); else Toptips("内容不能为空");
    },
    toChat: function() {
        var a = this;
        if (a.data.uid) if (a.data.uid == a.data.detail.seller_uid) a.data.disabled || a.openActionSheet(); else {
            var t = a.data.uid, e = a.data.detail.seller_uid;
            app.util.request({
                url: "entry/wxapp/item",
                cachetime: "0",
                data: {
                    act: "detail",
                    chat: 1,
                    id: a.data.item_id,
                    from_uid: t,
                    m: "superman_hand2"
                },
                success: function(t) {
                    t.data.errno ? a.showIconToast(t.errmsg) : wx.navigateTo({
                        url: "../chat/index?fromuid=" + e + "&itemid=" + a.data.item_id
                    });
                }
            });
        } else a.setData({
            showLogin: !0,
            chat: !0
        });
    },
    openActionSheet: function() {
        this.setData({
            show: !0
        });
    },
    closeActionSheet: function() {
        this.setData({
            show: !1
        });
    },
    handleActionClick: function(t) {
        var a = this, e = t.detail.index;
        if (0 == e || 1 == e) 0 == e ? a.setData({
            status: 2,
            tips: {
                top: "确定已转让吗？",
                bottom: "已转让物品将不能再次操作"
            },
            defaultBtn: !0,
            showLogin: !0
        }) : a.data.soldOut ? a.setData({
            status: 1,
            tips: {
                top: "确定上架吗？",
                bottom: ""
            },
            defaultBtn: !0,
            showLogin: !0
        }) : a.setData({
            status: -1,
            tips: {
                top: "确定下架吗？",
                bottom: "下架后别人将看不到物品",
                footer: "已下架物品将不再接收到聊天消息"
            },
            defaultBtn: !0,
            showLogin: !0
        }); else if (2 == e) {
            var i = a.data.item_id;
            wx.navigateTo({
                url: "../post/index?id=" + i
            }), a.setData({
                show: !1
            });
        }
    },
    goNext: function() {
        var a = this, e = a.data.item_id, i = a.data.status;
        app.util.request({
            url: "entry/wxapp/item",
            cachetime: "0",
            data: {
                act: "detail",
                id: e,
                status: i,
                m: "superman_hand2"
            },
            success: function(t) {
                t.data.errno ? a.showIconToast(t.errmsg) : (a.showIconToast("操作成功", "success"), 
                a.setData({
                    show: !1,
                    defaultBtn: !1,
                    showLogin: !1
                }), a.getItemDetail(e), 2 == i && a.setData({
                    disabled: !0
                }));
            }
        });
    },
    onShareAppMessage: function() {
        return {
            path: "/pages/detail/index?id=" + this.data.item_id
        };
    },
    showIconToast: function(t) {
        var a = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : "fail";
        Toast({
            type: a,
            message: t,
            selector: "#zan-toast"
        });
    }
});